﻿/**************************************************************************
创建时间:	2020/4/14
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

描	述： 文本框控件
记	录：
***************************************************************************/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using ZhCun.Utils.Helpers;

namespace ZhCun.Win.Controls
{
    public class EditText : TextBox, IEditDCM
    {
        [Browsable(false)]
        public virtual object RealValue
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value?.ToString();
            }
        }
        
        /// <summary>
        /// 默认构造
        /// </summary>
        public EditText()
        {
            EmptyTextTipColor = System.Drawing.Color.FromArgb(192, 192, 255);
        }

        const int WM_PAINT = 0xF;

        /// <summary>
        /// 空字符提示文本
        /// </summary>
        [Description("空字符提示文本")]
        public string EmptyTextTip { get; set; }
        /// <summary>
        /// 空字符提示文本颜色
        /// </summary>
        [Description("空字符文本的颜色")]
        public Color EmptyTextTipColor { get; set; }

        [Description("要绑定的拼音文本框")]
        [DefaultValue(null)]
        public EditText CodePYTextBox { get; set; }

        [Description("要绑定的五笔文本框")]
        [DefaultValue(null)]
        public EditText CodeWBTextBox { get; set; }

        object _ValueItem;

        /// <summary>
        /// 输入框的值 (替换之前用的Tag)
        /// </summary>
        [Browsable(false)]
        public object ValueItem
        {
            set
            {
                _ValueItem = value;
                IsValueChanged = true;
            }
            get
            {
                return _ValueItem;
            }
        }
        /// <summary>
        /// 输入框的显示内容
        /// </summary>
        [Browsable(false)]
        public virtual object DisplayItem
        {
            get
            {
                return RealValue;
            }
            set
            {
                RealValue = value;
            }
        }

        [Browsable(false)]
        public bool IsValueChanged { get; protected set; }

        [Browsable(false)]
        public bool IsDisplayChanged { get; protected set; }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_PAINT)
            {
                WmPaint(ref m);
            }
        }

        private void WmPaint(ref Message m)
        {
            //Rectangle rectangle = new Rectangle(0, 0, Width, Height);
            using (Graphics graphics = Graphics.FromHwnd(base.Handle))
            {
                if (base.Text.Length == 0
                   && !string.IsNullOrEmpty(EmptyTextTip)
                   && !Focused)
                {
                    TextFormatFlags format =
                         TextFormatFlags.EndEllipsis |
                         TextFormatFlags.VerticalCenter;
                    if (RightToLeft == RightToLeft.Yes)
                    {
                        format |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
                    }
                    TextRenderer.DrawText(
                        graphics,
                        EmptyTextTip,
                        Font,
                        base.ClientRectangle,
                        EmptyTextTipColor,
                          format);
                }
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            IsDisplayChanged = true;
        }

        protected override void OnLeave(EventArgs e)
        {
            if (CodePYTextBox != null)
            {
                CodePYTextBox.Text = Text.GetPYCode();
            }

            if (CodeWBTextBox != null)
            {
                CodeWBTextBox.Text = Text.GetWBCode();
            }

            base.OnLeave(e);
        }

        /// <summary>
        /// 设置默认状态，初始化 IsValueChanged 与 IsDisplayChanged
        /// </summary>
        public virtual void SetDefault()
        {
            Clear();
            ValueItem = null;
            IsValueChanged = false;
            IsDisplayChanged = false;
        }

    }
}